package com.ruoyi.radar.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.radar.domain.RadarSilo;
import com.ruoyi.radar.domain.RadarSiloResult;
import com.ruoyi.radar.service.RadarSiloResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/radarResult")
public class RadarSiloResultController extends BaseController {

    @Autowired
    private RadarSiloResultService radarSiloResultService;

    @PreAuthorize("@ss.hasPermi('radarSilo:result:list')")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam Map<String, Object> param)
    {
        Map<String, Object> result = new HashMap<>(2);
        List<RadarSiloResult> list = radarSiloResultService.selectSiloResult(param);
        Integer count = radarSiloResultService.getCount(param);
        result.put("data", list);
        result.put("total", count);
        return AjaxResult.success(result);
    }

    /**
     * 新增历史数据
     */
    @PreAuthorize("@ss.hasPermi('radarSilo:result:add')")
    @Log(title = "数据管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RadarSiloResult radarSiloResult)
    {
        try{
            radarSiloResult.setResultId(uuId());
            radarSiloResult.setCreateTime(currentTime());
            return toAjax(radarSiloResultService.save(radarSiloResult));
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }

    /**
     *修改料仓
     */
    @PreAuthorize("@ss.hasPermi('radarSilo:result:update')")
    @Log(title = "修改数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RadarSiloResult radarSiloResult)
    {
        try{
//            radarSilo.setUpdateTime(currentTime());
            return toAjax(radarSiloResultService.updateById(radarSiloResult));
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }

    /**
     * 删除料仓
     */
    @PreAuthorize("@ss.hasPermi('radarSilo:result:remove')")
    @Log(title = "删除数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{resultIds}")
    public AjaxResult remove(@PathVariable String[] resultIds)
    {
        ArrayList<String> ids = new ArrayList<>();
        Collections.addAll(ids, resultIds);
        return toAjax(radarSiloResultService.removeByIds(ids));
    }

    @Log(title = "历史数据导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('radarSilo:result:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Map<String, Object> param)
    {
        List<RadarSiloResult> list = radarSiloResultService.selectSiloResult(param);
//        List<GnssEquip> list = gnssEquipService.list(equip);
//        gnssEquipService
        ExcelUtil<RadarSiloResult> util = new ExcelUtil<RadarSiloResult>(RadarSiloResult.class);
        util.exportExcel(response, list, "历史数据");
    }
    //
    @Log(title = "料仓数据导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('radarSilo:result:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RadarSiloResult> util = new ExcelUtil<RadarSiloResult>(RadarSiloResult.class);
        List<RadarSiloResult> radarSiloResults = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = radarSiloResultService.importSilo(radarSiloResults, updateSupport, operName);
        return success(message);
    }
    //
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<RadarSiloResult> util = new ExcelUtil<RadarSiloResult>(RadarSiloResult.class);
        util.importTemplateExcel(response, "历史数据");
    }


}
